package com.mfkj.update.app

import android.app.Activity
import android.app.Application
import android.content.Context
import android.os.StrictMode
import androidx.startup.Initializer
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.PathUtils
import com.blankj.utilcode.util.Utils
import com.hjq.toast.ToastUtils
import com.hjq.toast.style.WhiteToastStyle
import com.tencent.mmkv.MMKV
import me.jessyan.autosize.AutoSizeConfig
import me.jessyan.autosize.onAdaptListener
import me.jessyan.autosize.unit.Subunits
import me.jessyan.autosize.utils.ScreenUtils
import java.io.File

class AppInitializer : Initializer<Application> {

    override fun create(context: Context): Application {
        Utils.init(context.applicationContext as Application)
        ToastUtils.init(context.applicationContext as Application, WhiteToastStyle())
        MMKV.initialize(CACHE_PATH)
        initDetectFileUriExposure()
        val config = LogUtils.getConfig()
        config.isSingleTagSwitch = true
        initAutoSize()
//        ViewPump.init(
//            ViewPump.builder()
//                .addInterceptor(
//                    CalligraphyInterceptor(
//                        CalligraphyConfig.Builder()
//                            .setDefaultFontPath("fonts/FZBIAOYSJW.TTF")
//                            .setFontAttrId(R.attr.fontPath)
//                            .build()
//                    )
//                )
//                .build()
//        )
        return Utils.getApp()
    }

    override fun dependencies(): List<Class<out Initializer<*>>> {
        return emptyList()
    }

    companion object {
        val CACHE_PATH = "${PathUtils.getInternalAppFilesPath()}${File.separator}app"


        //解决android7.0以上拍照崩溃问题
        private fun initDetectFileUriExposure() {
            val builder = StrictMode.VmPolicy.Builder()
            StrictMode.setVmPolicy(builder.build())
            builder.detectFileUriExposure()
        }


        private fun initAutoSize() {
            AutoSizeConfig.getInstance().unitsManager.supportSubunits = Subunits.PT
            AutoSizeConfig.getInstance().onAdaptListener = object : onAdaptListener {
                override fun onAdaptBefore(target: Any, activity: Activity?) {
                    // 使用以下代码, 可支持 Android 的分屏或缩放模式, 但前提是在分屏或缩放模式下当用户改变您 App 的窗口大小时
                    // 系统会重绘当前的页面, 经测试在某些机型, 某些情况下系统不会重绘当前页面, ScreenUtils.getScreenSize(activity) 的参数一定要不要传
                    // Application!!!
                    activity?.let {
                        AutoSizeConfig.getInstance().screenWidth =
                            ScreenUtils.getScreenSize(activity)[0]
                        AutoSizeConfig.getInstance().screenHeight =
                            ScreenUtils.getScreenSize(activity)[1]
                    }
                }

                override fun onAdaptAfter(target: Any, activity: Activity) {}
            }
        }

    }
}